package com.test
{
	import com.utils.ButtonModule;
	import com.utils.ExternalInterfaceUtil;
	import com.utils.ScrollBar;
	import com.utils.TextFieldUtil;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.utils.Timer;
	
	public class AsToJsTest extends Sprite
	{
		private var _inputTxt:TextField;
		private var _outputTxt:TextField;
		private var _senBtn:ButtonModule;
		private var _efu:ExternalInterfaceUtil;
		public function AsToJsTest()
		{
			this.addEventListener(Event.ADDED_TO_STAGE,onAddToStageHandler);
		}
		
		protected function onAddToStageHandler(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE,onAddToStageHandler);
			initFace();
			initData();
		}
		
		private function initFace():void
		{
			this.graphics.beginFill(0xCCCCCC);
			this.graphics.drawRect(0,0,this.stage.stageWidth,this.stage.stageHeight);
			this.graphics.endFill();
			_inputTxt = TextFieldUtil.newTextField(this,'',{x:20,y:10,type:TextFieldType.INPUT,textColor:0xFFFFFF,width:stage.stageWidth - 150,height:40,border:true,borderColor:0x0505050,background:true,backgroundColor:0,wordWrap:true});
			_senBtn = new ButtonModule(null,['发送']);
			this.addChild(_senBtn);
			_senBtn.x = _inputTxt.x + _inputTxt.width + 10;
			_senBtn.y = _inputTxt.y;
			_senBtn.addEventListener(MouseEvent.CLICK,onClickHandler);
			_outputTxt = TextFieldUtil.newTextField(this,'',{x:_inputTxt.x,y:_inputTxt.y + _inputTxt.height + 10,height:stage.stageHeight - _inputTxt.y - _inputTxt.height -20,width:_senBtn.x - _inputTxt.x + _senBtn.width,type:TextFieldType.DYNAMIC,textColor:0x00FF00,border:true,borderColor:0x0505050,background:true,backgroundColor:0,wordWrap:true},'',30);
		}
		
		private function onClickHandler(e:MouseEvent):void
		{
			if(_inputTxt.text == ''){
				_efu.sendToJS('showAlert','不能发送空消息');
			}else{
				_efu.sendToJS('sendToJavaScript',_inputTxt.text);
			}
		}	
		
		private function initData():void
		{
			_efu = ExternalInterfaceUtil.instance;
			if(ExternalInterface.available){
				try{
					_efu.receiveFromJS('receiveFromJavaScript',traceJsMessage);
					
					if(_efu.sendToJS('isReady')){
						_outputTxt.appendText('JavaScript初始化完毕!\n');
					}else{
						var timer:Timer = new Timer(100);
						timer.addEventListener(TimerEvent.TIMER,onTimerHandler);
						timer.start();
					}
				}catch(e:SecurityError){
					_outputTxt.appendText(e.message + '\n');
				}catch(e:Error){
					_outputTxt.appendText(e.message + '\n');
				}
			}else{
				_outputTxt.appendText('外部接口不可用!\n');
			}
		}
		
		private function traceJsMessage(value:String):void
		{
			_outputTxt.appendText('JavaScript说：' + value + '\n');
		}
		
		private function onTimerHandler(e:TimerEvent):void
		{
			_outputTxt.appendText('JavaScript初始化...\n');
			if(_efu.sendToJS('isReady')){
				_outputTxt.appendText('JavaScript初始化完毕!\n');	
				Timer(e.target).removeEventListener(TimerEvent.TIMER,onTimerHandler);
				Timer(e.target).stop();
			}
		}
	}
}